package org.danysoft.encoding_converter.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.apache.any23.encoding.TikaEncodingDetector;

public class CharsetRecognizer {

  public static Charset getCharsetByName(String name) throws IOException {
    return Charset.forName(name);
  }
  
  public static Charset recognizeCharset(InputStream is) throws IOException {
    return Charset.forName(new TikaEncodingDetector().guessEncoding(is));
  }

  public static Charset recognizeCharset(File f) throws IOException {
    
    InputStream is = new FileInputStream(f);
    try {
      return recognizeCharset(is);
    } finally {
      if (is != null) {
        is.close();
      }
    }
  }

}
